<!DOCTYPE html>
<%@ page language="java"  pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%> 
<html>
<head>
<title>代码表管理</title>
<jsp:include page="/common/head.jsp"></jsp:include>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>代码表管理</span>
        </div>
        <div id="toolbar" style="float: right"></div>
    </div>
    <div class="page-title-border"></div>
</div>

<div class="search-field">
    <span class="field-cont">
        <span class="field-cont-title">名称</span>
        <input type="text" class="summer-input" id="name"/>
    </span>
    
    <span class="field-cont">
        <span class="field-cont-title">代码</span>
        <input type="text" class="summer-input" id="code"/>
    </span>
    
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <col width="25%">
	    <col width="25%">
	    <col width="25%">
	    <col width="25%">
	    <thead>
		    <tr>
		        <th>名称</th>
				<th>代码</th>
				<th>类型</th>
		        <th>操作</th>
		    </tr>
	    </thead>
    </table>
</div>

<script>

	var CODE_CONST_URL = {
		LIST : basePath + '/sys/code/list',
		INIT_ADD : basePath + '/sys/code/initAdd',
		INIT_EDIT : basePath + '/sys/code/initEdit',
		DEL : basePath + '/sys/code/delete',
		INIT_DETAIL : basePath + '/sys/codeDetail/initDetatil'
	};
	var permission = CommonUtil.getPermission('code');
	
	$(function() {
		// 新建toobar
		$.funcbar({
			id: 'toolbar',
			type: 'toolbar',
			data: permission,
			permit: ['code:initAdd'],
		});

		//初始化表格
		createTable();

		//查询
		$('#search').on('click', reloadTable);

		//重置
		$('#reset').on('click', function() {
			$('#name').val('');
			$('#code').val('');
			reloadTable();
		});

		//新建
		$('#toolbar').on('click', 'a[data-permit="code:initAdd"]', function() {
			window.location.href = CODE_CONST_URL.INIT_ADD;
		});
		
		//编辑
		$('#t-wrap').on('click', 'a[data-permit="code:initEdit"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			
			window.location.href = CODE_CONST_URL.INIT_EDIT + '?id=' + id;
		});
		
		//删除
		$('#t-wrap').on('click', 'a[data-permit="code:delete"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除选中的记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CODE_CONST_URL.DEL + '?ids=' + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
		
		//配置列表项
		$('#t-wrap').on('click', 'a[data-permit="code:initDetail"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			
			window.location.href = CODE_CONST_URL.INIT_DETAIL + '?id=' + id;
		});
	});

	// 重新加载表格
	function reloadTable() {
		$('#dataList').datagrid('reload', {
			ajax : {
				param : {
					"name" : trimSpace($('#name').val()),
					"code" : trimSpace($('#code').val())
				}
			}
		});
	}

	//初始化列表
	function createTable() {
		$('#dataList').datagrid({
			ajax : {
				url : CODE_CONST_URL.LIST,
				param : {
					"name" : trimSpace($('#name').val()),
					"code" : trimSpace($('#code').val())
				}
			},
			columns : [ 
			  {
				field : 'id',
				hidden : true
			  }, 
			  {
				field : 'name'
			  }, 
			  {
				field : 'code'
			  }, 
			  {
				field : 'type',
				render : function(rowData) {
					return rowData.type == '1' ? '级联' : '非级联';
				}
			  }, 
			  {
				render : function(rowData) {
					return $.funcbar({
						data : permission, //请求的数据
						type : 'text', //类型
						permit : [ 'code:initEdit', 'code:delete', 'code:initDetail' ]//需要的权限按钮
					});
				}
			  }

			]
		});
	}
</script>
</body>
</html>
